package com.wangzeqi.changliaoba.login;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.easemob.chat.EMChatManager;
import com.easemob.chat.EMContactManager;
import com.easemob.exceptions.EaseMobException;
import com.wangzeqi.changliaba.dao.ContactsEntityDao;
import com.wangzeqi.changliaba.dao.UserPage;
import com.wangzeqi.changliaoba.R;
import com.wangzeqi.changliaoba.base.BaseActivity;
import com.wangzeqi.changliaoba.singleton.AllSingleTon;

import java.util.List;

import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.listener.FindListener;
import de.greenrobot.dao.query.QueryBuilder;

/**
 * Created by dllo on 16/3/10.
 */
public class ScannerAddFriendActivity extends BaseActivity implements View.OnClickListener {
    private ImageView scanner_friend_detail_back, scanner_friend_icon;
    private TextView scanner_friend_remarks, scanner_friend_id_detail;
    private Button scanner_send_msg_add_friend;
    String scannerGetName;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_scannner_add_friend);
        scanner_friend_detail_back = (ImageView) findViewById(R.id.scanner_friend_detail_back);
        scanner_friend_icon = (ImageView) findViewById(R.id.scanner_friend_icon);
        scanner_friend_remarks = (TextView) findViewById(R.id.scanner_friend_remarks);
        scanner_friend_id_detail = (TextView) findViewById(R.id.scanner_friend_id_detail);
        scanner_send_msg_add_friend = (Button) findViewById(R.id.scanner_send_msg_add_friend);


        Intent intent = getIntent();
        scannerGetName = intent.getStringExtra("scannerName");
        scanner_friend_id_detail.setText(scannerGetName);
        getNickName();
        scanner_friend_detail_back.setOnClickListener(this);
        scanner_send_msg_add_friend.setOnClickListener(this);
    }


    private void getNickName() {

        BmobQuery<UserPage> query = new BmobQuery<UserPage>();
        //查询username 是当前用户的数据
        query.addWhereEqualTo("username", scannerGetName);
        //如果不加上这条语句，默认返回10条数据
        query.setLimit(1);
        query.findObjects(this, new FindListener<UserPage>() {
            @Override
            public void onSuccess(List<UserPage> list) {

                for (UserPage userPage : list) {
                    //获得昵称的信息
                    scanner_friend_remarks.setText(userPage.getNick_name());
                }
            }

            @Override
            public void onError(int i, String s) {
            }
        });
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.scanner_friend_detail_back:
                finish();
                break;
            case R.id.scanner_send_msg_add_friend:
                //判断对方是不是你的好友，是就不用添加
                List<String> listContact;
                try {
                    listContact = EMContactManager.getInstance().getContactUserNames();
                    for (int i = 0; i < listContact.size(); i++) {
                        if (listContact.get(i).equals(scannerGetName)) {
                            Toast.makeText(this, "对方已经是你的好友，不能重复添加，请点击返回键退出", Toast.LENGTH_SHORT).show();
                        } else {
                            try {
                                EMContactManager.getInstance().addContact(scannerGetName, "对方请求添加你为好友");
                            } catch (EaseMobException e) {
                                e.printStackTrace();
                            }
                            finish();
                        }
                    }
                } catch (EaseMobException e) {
                    e.printStackTrace();
                }

                break;
        }
    }
}
